const _form = this.getApiPage().retrievePlugin('base-forms');
const $form = this.pageObj.getApiWidgetByAliasName('form_14');
const data = _form.getFormData('form_23');
const value = _form.getFormData('form_14');
const modals = this.pageObj.getApiWidgetsByClassName('optss-preset');
const variables = this.getApiPage().getVariables();
const proxy = variables.find(item => item.name === 'environmentType')?.value || '';
console.log('save', value, data);
const params = {
  equipmentId: value.id,
  presetName: data.name,
  presetNum: +value.list
};
const options = {
  method: 'POST',
  headers: {
    "Content-type": "application/json",
  },
  body: JSON.stringify(params)
};

const params1 = {
  command: 'SET_PRESET',
  equipmentId: value.id,
  presetNum: +value.list
};

fetch(`/${proxy}IotDdevice/project-business-iot-service/hikvision/sec/{equipmentId}/preset/set`, options)
  .then(res => res.json())
  .then(json => {
    if (json.success) {
      modals.forEach(v => v.hideWidget())

      // 设置预置位
      // fetch('/IotDdevice/project-business-iot-service/hikvision/sdk/presetControl', options1)
      //     .then(res => res.json())
      //     .then(json =>{
      //         if(json.success){
      //             window.layer.msg('预置位设置成功')
      //         }
      //     })

      // 获取预置位列表
      const options2 = {
        method: 'POST',
        headers: {
          "Content-type": "application/json",
        },
        body: JSON.stringify({ equipmentId: value.id })
      }
      fetch(`/${proxy}IotDdevice/project-business-iot-service/hikvision/sdk/queryPresets`, options2)
        .then(res => res.json())
        .then(json => {
          if (json.success) {
            console.log('_form', _form)
            const list = json.result.map(v => ({
              ...v,
              sourceName: v.name,
              name: v.presetNum + '-' + v.name,
              value: v.presetNum
            }))
            $form.widgetObj._option.options = {
              list
            }
            this.pageObj.setVariables([{ name: 'device_list', value: list }])
            $form.widgetObj.render()
            _form.setFormData('form_14', {
              id: params1.equipmentId,
              list: params1.presetToken
            })
            console.log('data', _form.getFormData('form_14'))
          }
        })
    }
  })
